<?php
  //Loader::loadFile('config.php', 'modules/VoteThailand');
  /**
  * initialise module
  *
  */
  function VoteDataCenter_init() {

    //Create votedatacenter_province table
    if (!DBUtil::createTable('votedatacenter_province')) {
        return false;
    }
    //Create votedatacenter_district table
    if (!DBUtil::createTable('votedatacenter_district')) {
        return false;
    }
    //Create votedatacenter_province table
    if (!DBUtil::createTable('votedatacenter_tambon')) {
        return false;
    }
    //Create votedatacenter_village table
    if (!DBUtil::createTable('votedatacenter_village')) {
        return false;
    }

    //Create votedatacenter_house_no table
    if (!DBUtil::createTable('votedatacenter_house_no')) {
        return false;
    }

    //Create votedatacenter_gender table
    if (!DBUtil::createTable('votedatacenter_gender')) {
        return false;
    }

    //Create votedatacenter_person table
    if (!DBUtil::createTable('votedatacenter_person')) {
        return false;
    }

    //Create votedatacenter_party table
    if (!DBUtil::createTable('votedatacenter_party')) {
        return false;
    }

    //Create votedatacenter_candidate table
    if (!DBUtil::createTable('votedatacenter_candidate')) {
        return false;
    }

    //Create votedatacenter_person_party table
    if (!DBUtil::createTable('votedatacenter_person_party')) {
        return false;
    }

    //Create votedatacenter_person_leader_canvasser table
    if (!DBUtil::createTable('votedatacenter_person_leader_canvasser')) {
        return false;
    }

    //Create votedatacenter_person_canvasser table
    if (!DBUtil::createTable('votedatacenter_person_canvasser')) {
        return false;
    }

    //Create votedatacenter_person_canvasser table
    if (!DBUtil::createTable('votedatacenter_project')) {
        return false;
    }



    //Create votedatacenter_excel table
    if (!DBUtil::createTable('votedatacenter_excel')) {
        return false;
    }


    //Create votedatacenter_excel table
    if (!DBUtil::createTable('votedatacenter_extperson')) {
        return false;
    }


    VoteDataCenter_init_data();
    VoteDataCenter_init_folder();
    return true;
  }

  function VoteDataCenter_init_folder(){

    if (!is_dir("pnTemp/votedatacenter_upload/")) {
      mkdir("pnTemp/votedatacenter_upload/", 0755);
    }
    
  }

  function VoteDataCenter_delete() {

    DBUtil::dropTable('votedatacenter_province');
    DBUtil::dropTable('votedatacenter_district');
    DBUtil::dropTable('votedatacenter_tambon');
    DBUtil::dropTable('votedatacenter_village');
    DBUtil::dropTable('votedatacenter_house_no');
    DBUtil::dropTable('votedatacenter_gender');
    DBUtil::dropTable('votedatacenter_person');
    DBUtil::dropTable('votedatacenter_party');
    DBUtil::dropTable('votedatacenter_candidate');
    DBUtil::dropTable('votedatacenter_person_party');
    DBUtil::dropTable('votedatacenter_person_leader_canvasser');
    DBUtil::dropTable('votedatacenter_person_canvasser');
    DBUtil::dropTable('votedatacenter_project');
    DBUtil::dropTable('votedatacenter_head_people');
    
    DBUtil::dropTable('votedatacenter_excel');
    DBUtil::dropTable('votedatacenter_extperson');
/*
 DROP TABLE `zk_votedatacenter_candidate`;
 DROP TABLE `zk_votedatacenter_district`;
 DROP TABLE `zk_votedatacenter_gender`;
 DROP TABLE `zk_votedatacenter_house_no`;
 DROP TABLE `zk_votedatacenter_party`;
 DROP TABLE `zk_votedatacenter_person`;
 DROP TABLE `zk_votedatacenter_person_canvasser`;
 DROP TABLE `zk_votedatacenter_person_leader_canvasser`;
 DROP TABLE `zk_votedatacenter_person_party`;
 DROP TABLE `zk_votedatacenter_province`;
 DROP TABLE `zk_votedatacenter_tambon`;
 DROP TABLE `zk_votedatacenter_village`;
 DROP TABLE `zk_votedatacenter_project`;


CREATE TABLE `zk_votedatacenter_extperson` (
  `exp_id` BIGINT(11) NOT NULL AUTO_INCREMENT,
  `exp_pro_id` INT(11) NOT NULL,
  `exp_dis_id` INT(11) NOT NULL,
  `exp_tam_id` INT(11) NOT NULL,
  `exp_vil_id` INT(11) NOT NULL,
  `exp_vil_moo` INT(11) NOT NULL,
  `exp_hou_id` INT(11) NOT NULL,
  `exp_gen_id` INT(11) NOT NULL,
  `exp_pid` VARCHAR(255) DEFAULT NULL,
  `exp_firstname` VARCHAR(255) DEFAULT NULL,
  `exp_lastname` VARCHAR(255) DEFAULT NULL,
  `exp_telephone` VARCHAR(255) DEFAULT NULL,
  `exp_mobilephone` VARCHAR(255) DEFAULT NULL,
  `exp_birthdate` DATE DEFAULT NULL,
  `exp_obj_status` VARCHAR(1) NOT NULL DEFAULT 'A',
  `exp_cr_date` DATETIME NOT NULL DEFAULT '1970-01-01 00:00:00',
  `exp_cr_uid` INT(11) NOT NULL DEFAULT '0',
  `exp_lu_date` DATETIME NOT NULL DEFAULT '1970-01-01 00:00:00',
  `exp_lu_uid` INT(11) NOT NULL DEFAULT '0',
  PRIMARY KEY  (`exp_id`),
  KEY `idx_exp_pro_id` (`exp_pro_id`),
  KEY `idx_exp_dis_id` (`exp_dis_id`),
  KEY `idx_exp_tam_id` (`exp_tam_id`),
  KEY `idx_exp_vil_id` (`exp_vil_id`),
  KEY `idx_exp_vil_moo` (`exp_vil_moo`),
  KEY `idx_exp_hou_id` (`exp_hou_id`),
  KEY `idx_exp_gen_id` (`exp_gen_id`),
  KEY `idx_exp_pid` (`exp_pid`)
) ENGINE=MYISAM DEFAULT CHARSET=utf8


*/
    return true;
  }

   function VoteDataCenter_init_data(){
    setlocale ( LC_ALL, 'en_US.UTF-8' );
    //Init gender
    $genderArray = array(
                          array('id'        => '1',
                                'name'      => 'ชาย'),
                          array('id'        => '2',
                                'name'      => 'หญิง')
                        );
    DBUtil::insertObjectArray($genderArray, 'votedatacenter_gender', true);
    unset($genderArray);
    unset($data);

    //Init province data
    $file = "modules/VoteDataCenter/data/province.csv";
    $handle = fopen($file, "r");
    if($handle!==false){
      $objArray =array();
      while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
          $provinceArray[] = array(
                                    'id'        => $data[0],
                                    'name'      => VoteDataCenter_fixEncoding($data[1]),
                                    'latitude'  => $data[2],
                                    'longitude' => $data[3]
                                  );
      }
      DBUtil::insertObjectArray($provinceArray, 'votedatacenter_province', true);
      unset($provinceArray);
      unset($data);
      fclose($handle);
    }

    //Init district data
    $file = "modules/VoteDataCenter/data/district.csv";
    $handle = fopen($file, "r");
    if($handle!==false){
      $districtArray =array();
      while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            $districtArray[] = array(
                                      'id'        => $data[0],
                                      'pro_id'    => $data[1],
                                      'name'      => $data[2],
                                      'latitude'  => $data[3],
                                      'longitude' => $data[4]
                                    );
      }
      //DBUtilEx::setCharacterSet('utf8');
      DBUtil::insertObjectArray($districtArray, 'votedatacenter_district', true);
      unset($districtArray);
      unset($data);
      fclose($handle);
    }


    //Init tambon data
    $file = "modules/VoteDataCenter/data/tambon.csv";
    $handle = fopen($file, "r");
    if($handle!==false){
      $tambonArray =array();
      while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            $tambonArray[] = array(
                                      'id'        => $data[0],
                                      'pro_id'    => $data[1],
                                      'dis_id'    => $data[2],
                                      'name'      => $data[3],
                                      'latitude'  => $data[4],
                                      'longitude' => $data[5]
                                    );
      }
      DBUtil::insertObjectArray($tambonArray, 'votedatacenter_tambon', true);
      unset($tambonArray);
      unset($data);
      fclose($handle);
    }

    //Init village data
    $file = "modules/VoteDataCenter/data/village.csv";
    $handle = fopen($file, "r");
    if($handle!==false){
      $villageArray =array();
      while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            $villageArray[] = array(
                                      'id'        => $data[0],
                                      'pro_id'    => $data[1],
                                      'dis_id'    => $data[2],
                                      'tam_id'    => $data[3],
                                      'moo'       => $data[4],
                                      'name'      => $data[5],
                                      'latitude'  => $data[6],
                                      'longitude' => $data[7]
                                    );
      }
      DBUtil::insertObjectArray($villageArray, 'votedatacenter_village', true);
      unset($villageArray);
      unset($data);
      fclose($handle);
    }

    //Init house no. data
    $file = "modules/VoteDataCenter/data/house_no.csv";
    $handle = fopen($file, "r");
    if($handle!==false){
      $houseNoArray =array();
      while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            $houseNoArray[] = array(
                                      'id'        => $data[0],
                                      'pro_id'    => $data[1],
                                      'dis_id'    => $data[2],
                                      'tam_id'    => $data[3],
                                      'vil_id'    => $data[4],
                                      'moo'       => $data[5],
                                      'unit'      => $data[6],
                                      'no'        => $data[7],
                                      'trok'      => $data[8],
                                      'soi'       => $data[9],
                                      'road'      => $data[10],
                                      'zipcode'   => $data[11],
                                      'latitude'  => $data[12],
                                      'longitude' => $data[13]
                                    );
      }
      DBUtil::insertObjectArray($houseNoArray, 'votedatacenter_house_no', true);
      unset($houseNoArray);
      unset($data);
      fclose($handle);
    }

    //Init person data
    $file = "modules/VoteDataCenter/data/person.csv";
    $handle = fopen($file, "r");
    if($handle!==false){
      $personArray =array();
      while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            $personArray[] = array(
                                      'id'          => $data[0],
                                      'pro_id'      => $data[1],
                                      'dis_id'      => $data[2],
                                      'tam_id'      => $data[3],
                                      'vil_id'      => $data[4],
                                      'vil_moo'     => $data[5],
                                      'hou_id'      => $data[6],
                                      'gen_id'      => $data[7],
                                      'pid'         => $data[8],
                                      'firstname'   => $data[9],
                                      'lastname'    => $data[10],
                                      'telephone'   => $data[11],
                                      'mobilephone' => $data[12],
                                      'birthdate'   => $data[13]
                                    );
      }
      DBUtil::insertObjectArray($personArray, 'votedatacenter_person', true);
      unset($personArray);
      unset($data);
      fclose($handle);
    } 

  }

  function VoteDataCenter_fixEncoding($in_str) {
    $cur_encoding = mb_detect_encoding($in_str) ;

    if($cur_encoding == "UTF-8" && mb_check_encoding($in_str,"UTF-8"))
      return $in_str;
    else
      return utf8_encode($in_str);
  } 

